package com.magic233.controller;

import com.magic233.entity.*;
import com.magic233.service.AdminsService;
import com.magic233.service.OrdersService;
import com.magic233.service.UsersService;
import com.magic233.service.WorkersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * ClassName: AdminController
 * Description:
 * date: 2021/5/30 13:42
 * author: zouyuan
 */

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private WorkersService workersService;

    @Autowired
    private AdminsService adminsService;

    @Autowired
    private UsersService usersService;

    //工单指派
    @RequestMapping("/sendOrder")
    public int sendOrder(HttpServletRequest request, Long id, Long wid) {
        System.out.println("管理员指派工单");
        if (id == null || wid == null || wid == 0) {
            System.out.println("参数为空");
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        int re = ordersService.sendOrder(user, id, wid);
        return re;
    }

    //处理退单申请--同意
    @RequestMapping("/handOrderYes")
    public int handOrderYes(HttpServletRequest request, Long id) {
        System.out.println("管理员同意退单");
        if (id == null) {
            System.out.println("参数为空");
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        int re = ordersService.handOrderYes(user, id);
        return re;
    }

    //处理退单申请--拒绝
    @RequestMapping("/handOrderNo")
    public int handOrderNo(HttpServletRequest request, Long id, String adminReason) {
        System.out.println("管理员拒绝退单");
        if (id == null || adminReason == null || "".equals(adminReason)) {
            System.out.println("参数为空");
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        int re = ordersService.handOrderNo(user, id, adminReason);
        return re;
    }

    //查询工单
    @RequestMapping("/selectOrders")
    public Object selectOrders(HttpServletRequest request, String status, String type, Long startTime, Long endTime, Integer pageNum, Integer pageSize) {
        Users user = (Users) request.getAttribute("user");
        Orders order = new Orders();
        if (status == null || "".equals(status)) {
            order.setStatus("全部状态");
            System.out.println("设置全部状态");
        } else {
            order.setStatus(status);
            System.out.println("设置" + status);
        }
        if (type == null || "".equals(type)) {
            order.setType("全部类别");
            System.out.println("设置全部类别");
        } else {
            order.setType(type);
            System.out.println("设置" + type);
        }
        order.setSelectStartTime(startTime);
        order.setSelectEndTime(endTime);
        Map<Object, Object> map = ordersService.selectOrders(order, pageNum, pageSize);
        return map;
    }

    //管理员查询启用的维修工人
    @RequestMapping("/selectWorkers2")
    public Object selectWorkers2() {
        List<Workers> workers = workersService.selectWorkers2();
        return workers;
    }

    //管理员查询所有维修工人
    @RequestMapping("/selectWorkers")
    public Object selectWorkers() {
        List<Workers> workers = workersService.selectWorkers();
        return workers;
    }

    //查询管理人员
    @RequestMapping("/selectAdmins")
    public Object selectAdmins() {
        List<Admins> admins = adminsService.selectAdmins();
        return admins;
    }

    //查询部门负责人
    @RequestMapping("/selectManagers")
    public Object selectManagers() {
        List<Users> users = usersService.selectUserByPower();
        return users;
    }

    //管理员撤单
    @RequestMapping("/removeOrder")
    public Object removeOrder(HttpServletRequest request, Long id) {
        System.out.println("管理员撤销工单");
        if (id == null) {
            return 0;
        }

        Users user = (Users) request.getAttribute("user");
        int re = ordersService.adminRemoveOrder(user, id);
        return re;
    }

    //工单完成情况统计
    @RequestMapping("/orderStatistical")
    public Object orderStatistical(Long startTime, Long endTime) {
        System.out.println("管理员查询工单统计");
        Orders order = new Orders();
        order.setSelectStartTime(startTime);
        order.setSelectEndTime(endTime);
        List<OrderStatistical> orderStatisticals = ordersService.orderStatistical(order);
        return orderStatisticals;
    }

    //工人完成情况统计
    @RequestMapping("/workerStatistical")
    public Object workerStatistical() {
        System.out.println("管理员查询工人统计");
        List<Workers> workerStatisticals = ordersService.workerStatistical();
        return workerStatisticals;
    }

    //预警天数查询
    @RequestMapping("/selectWarnSet")
    public Object selectWarnSet(HttpServletRequest request) {
        System.out.println("管理员查询预警天数");
        Users user = (Users) request.getAttribute("user");
        Admins admins = adminsService.selectWarnSet(user.getId());
        return admins;
    }

    //预警天数设置
    @RequestMapping("/warnSet")
    public Object warnSet(HttpServletRequest request, String blueNum, String yellowNum, String redNum) {
        System.out.println("管理员设置预警天数");
        if (blueNum == null || yellowNum == null || redNum == null || "".equals(blueNum) || "".equals(yellowNum) || "".equals(redNum)) {
            System.out.println("参数为空");
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        Admins admin = new Admins();
        admin.setId(user.getId());
        admin.setBluenum(Integer.valueOf(blueNum));
        admin.setYellownum(Integer.valueOf(yellowNum));
        admin.setRednum(Integer.valueOf(redNum));
        int re = adminsService.warnSet(admin);
        return re;

    }


    //工单预警
    @RequestMapping("/warning")
    public Object warning(Long startTime, Long endTime, String type, Long num1, Long num2, Integer pageNum, Integer pageSize) {
        if (startTime == null || endTime == null || type == null || num1 == null || num2 == null) {
            System.out.println("参数为空");
            return null;
        }
        Orders order = new Orders();
        System.out.println("type" + type);
        if (type == null || "".equals(type)) {
            order.setType("全部类别");
            System.out.println("设置全部类别");
        } else {
            order.setType(type);
            System.out.println("设置" + type);
        }
        order.setSelectStartTime(startTime);
        order.setSelectEndTime(endTime);
        Long left = num1 * 86400000;
        Long right = num2 * 86400000;
        order.setLeft(left);
        order.setRight(right);

        Map<Object, Object> objectObjectMap = ordersService.selectOrdersWarn(order, pageNum, pageSize);
        return objectObjectMap;
    }
}
